package com.yky.service.impl;

/**
 * 业务聚合
 *
 * @version [V2.0]
 * @package com.example.sso.server.service.impl;
 * @description:[用一句话描述该文件做什么]
 * @author: [zhuben]
 * @date: [2024-01-24 17:00]
 * @copyright: 2024 echovin.com
 */

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yky.dao.LoginInfoDao;
import com.yky.model.LoginInfoModel;
import com.yky.service.LoginService;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class LoginServiceImpl extends ServiceImpl<LoginInfoDao, LoginInfoModel> implements LoginService {


    @Override
    public String getByKey(String key) {
        if (key == null || "".equals(key)){
            return null;
        }
        LambdaQueryWrapper<LoginInfoModel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(LoginInfoModel::getKeystr, key);
        LoginInfoModel loginInfoModel = this.getOne(queryWrapper);
        if (loginInfoModel == null){
            return null;
        }
        Date expireTime = loginInfoModel.getExpireTime();
        if (expireTime != null && expireTime.getTime() < System.currentTimeMillis()){
            this.removeById(loginInfoModel.getId());
        }
        return loginInfoModel.getResult();
    }

    @Override
    public LoginInfoModel getModelByKey(String key) {
        if (key == null || "".equals(key)){
            return null;
        }
        LambdaQueryWrapper<LoginInfoModel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(LoginInfoModel::getKeystr, key);
        return this.getOne(queryWrapper);
    }

    @Override
    public void deleteByKey(String key) {
        if ("".equals(key) || key == null){
            return;
        }
        LambdaQueryWrapper<LoginInfoModel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(LoginInfoModel::getKeystr, key);
        this.remove(queryWrapper);
    }

    @Override
    public Set<String> selectLikeByKey(String keyword) {
        if ("".equals(keyword) || null == keyword){
            return new HashSet<>();
        }
        LambdaQueryWrapper<LoginInfoModel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(LoginInfoModel::getKeystr, keyword);
        List<LoginInfoModel> list = this.list(queryWrapper);
        if (list == null || list.size() <= 0){
            return new HashSet<>();
        }
        return list.stream().filter(s->s != null && s.getKeystr() != null && !"".equals(s.getKeystr())).map(s->s.getKeystr()).collect(Collectors.toSet());
    }

    @Override
    public void removeByKey(String key) {
        if ("".equals(key) || null == key){
            return;
        }
        LambdaQueryWrapper<LoginInfoModel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(LoginInfoModel::getKeystr, key);
        this.remove(queryWrapper);
    }
}
